package com.tian.service.impl.manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tian.enums.OrderStatusEnum;
import com.tian.mapper.OrderManageMapperCustom;
import com.tian.pojo.vo.OrderManageVO;
import com.tian.pojo.vo.UserManageVO;
import com.tian.service.manage.OrderManageService;
import com.tian.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderManageServiceImpl implements OrderManageService {

    @Autowired
    private OrderManageMapperCustom orderManageMapperCustom;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryPagedOrderManage(Integer page, Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page,pageSize);
        List<OrderManageVO> list = orderManageMapperCustom.queryPagedOrderManage();
        for (OrderManageVO orderManageVO:list){
            Integer orderStatus = orderManageVO.getOrderStatus();
            if (orderStatus== OrderStatusEnum.WAIT_PAY.type){
                orderManageVO.setOrderStatusStr(OrderStatusEnum.WAIT_PAY.value);
            }
            if (orderStatus == OrderStatusEnum.WAIT_DELIVER.type){
                orderManageVO.setOrderStatusStr(OrderStatusEnum.WAIT_DELIVER.value);
            }
            if (orderStatus==OrderStatusEnum.WAIT_RECEIVE.type){
                orderManageVO.setOrderStatusStr(OrderStatusEnum.WAIT_RECEIVE.value);
            }
            if (orderStatus==OrderStatusEnum.SUCCESS.type){
                orderManageVO.setOrderStatusStr(OrderStatusEnum.SUCCESS.value);
            }
            if (orderStatus==OrderStatusEnum.CLOSE.type){
                orderManageVO.setOrderStatusStr(OrderStatusEnum.CLOSE.value);
            }
        }

        return setterPagedGrid(list,page);

    }

    @Override
    public List<OrderManageVO> getOrderManageByOrderId(String orderId) {
        OrderManageVO orderManageVO = orderManageMapperCustom.getOrderManageByOrderId(orderId);
        List<OrderManageVO> list=new ArrayList<>();
        list.add(orderManageVO);
        return list;
    }

    private PagedGridResult setterPagedGrid(List<?> list,Integer page){
        PageInfo<?> pageList =new PageInfo<>(list);
        PagedGridResult grid=new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
